package org.noip2.noskamaru.katsys;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Currency {
	
	private static int port=4445;
	private static PrintWriter out;
	private static BufferedReader in;
	private static URL url;
	private static String date;
	public static void main(String args[]) throws ParserConfigurationException, 
	IOException, SAXException	{
		
		
		ServerSocket ss=new ServerSocket(port);
		System.out.println("service is running...");
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		url=new URL("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
		InputStream IS =new BufferedInputStream(url.openStream());
		Document doc = builder.parse(IS);
		NodeList list = doc.getElementsByTagName("Cube");
		Element felement = (Element) list.item(1);
		Node fattr=felement.getAttributeNode("time");
		date=fattr.getTextContent();
		
		
		
		try	{
			while(true)	{
				Socket s=null;
				
				try		{
					float amount=0;
					s=ss.accept();
					out=new PrintWriter(s.getOutputStream(),true);
					in=new BufferedReader(new InputStreamReader(s.getInputStream()));
					String inamount=in.readLine();
					try		{
						amount=Float.parseFloat(inamount);
					}
					catch(Exception e)	{
						amount=0;
					}
					
					Element felement2 = (Element) list.item(1);
					Node fattr2=felement.getAttributeNode("time");
					if(!fattr2.getTextContent().equals(fattr))	{
						date=fattr2.getTextContent();
            url=new URL("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
            InputStream IS =new BufferedInputStream(url.openStream());
            Document doc = builder.parse(IS);
                  
					}
				
					// Retrieving cube XML nodes
					NodeList list2 = doc.getElementsByTagName("Cube");
					for (int i = 2; i < list2.getLength(); i++) {
						
						Element element = (Element) list2.item(i);
						NamedNodeMap attr = element.getAttributes();
						for (int j=1;j<=attr.getLength();j+=2) {
							String num=attr.item(j).getTextContent();
							float number=Float.parseFloat(num);
							double newNum=number*amount;
							newNum = Math.round(newNum*100.0)/100.0;
							out.println(newNum);

						}
					
					}
					out.println(date);
					
					
					
				}
				finally		{
					if(in !=null)	{
						in.close();
					}
					if(out != null)		{
						out.close();
					}
					
				}
			}
		}
		finally		{
			if(ss!=null)	{
				ss.close();
			}
		}
		
	}
	
	

}
